package com.rodrigo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rodrigo.model.Time;
import com.rodrigo.service.TimeService;

@Controller
@RequestMapping(value="/time")
public class TimeController {
	
	@Autowired
	private TimeService timeService;
	
	@RequestMapping(value="/time/add", method=RequestMethod.GET)
	public ModelAndView addtimePage() {
		ModelAndView modelAndView = new ModelAndView("add-time");
		modelAndView.addObject("time", new Time());
		return modelAndView;
	}
	
	@RequestMapping(value="/time/add/process", method=RequestMethod.POST)
	public ModelAndView addingtime(@ModelAttribute Time time) {
		
		ModelAndView modelAndView = new ModelAndView("index");
		timeService.addTime(time);
		
		String mensagem = "Time adicionado com sucesso.";
		modelAndView.addObject("mensagem", mensagem);
		
		return modelAndView;
	}
	
	@RequestMapping(value="/time/lista")
	public ModelAndView listatimes() {
		ModelAndView modelAndView = new ModelAndView("lista-time");
		
		List<Time> times = timeService.getTimes();
		modelAndView.addObject("times", times);
		
		return modelAndView;
	}
	
	@RequestMapping(value="/time/edit/{id}", method=RequestMethod.GET)
	public ModelAndView edittimePage(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView("editar-time");
		Time time = timeService.getTime(id);
		modelAndView.addObject("time",time);
		return modelAndView;
	}
	
	@RequestMapping(value="/time/edit/{id}", method=RequestMethod.POST)
	public ModelAndView edditingtime(@ModelAttribute Time time, @PathVariable Integer id) {
		
		ModelAndView modelAndView = new ModelAndView("editar-time");
		
		timeService.updateTime(time);
		
		String mensagem = "time editado com sucesso.";
		modelAndView.addObject("mensagem", mensagem);
		
		return modelAndView;
	}
	
	@RequestMapping(value="/time/delete/{id}", method=RequestMethod.GET)
	public ModelAndView deletetime(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView("index");
		timeService.deleteTime(id);
		String mensagem = "time deletado com sucesso.";
		modelAndView.addObject("mensagem", mensagem);
		return modelAndView;
	}

}


